//========================================================================
/**
 * Author   : cuisw <shaovie@gmail.com>
 * Date     : 2008-10-18 00:27
 */
//========================================================================

#ifndef _ASYNCNIOIMPL_H_
#define _ASYNCNIOIMPL_H_
#include "Pre.h"

#include "GlobalMacros.h"

class Proactor;
class ProactorImpl;
class MessageBlock;
class AsynchHandler;

/**
 * @class AsynchResultImpl
 *
 * @brief Abstract base class for the all the classes that provide
 * concrete implementations for AsynchResult.
 */
class AsynchResultImpl
{
    friend class EpollProactor;
    
    friend class EpollProactorWriteTask;
public:
    virtual ~AsynchResultImpl ();

    // Number of bytes transferred by the operation.
    virtual size_t bytes_transferred (void) const = 0;

    // Did the operation succeed?
    virtual int success (void) const = 0;

    virtual const void *completion_key (void) const = 0;

    /**
     * On WIN32, this returns the event associated with the OVERLAPPED
     * structure.
     * This returns NDK_INVALID_HANDLE on POSIX4-Unix platforms.
     */
    virtual NDK_HANDLE event (void) const = 0;

    virtual long long offset (void) const = 0;

    // Error value if the operation fail.
    virtual int error (void) const = 0;

protected:
    virtual int complete (size_t bytes_transferred,
	    int success,
	    const void *completion_key,
	    int error) = 0;
    //virtual int complete (void) = 0;

    // Post this to the Proactor's completion port.
    //virtual int post_completion (ProactorImpl *proactor) = 0; !!!

    // Do-nothing constructor.
    AsynchResultImpl ();
};
/**
 * @class AsynchOpImpl
 *
 * @brief Abstract base class for all the concrete implementation
 * classes that provide different implementations for the 
 * AsynchOpt.
 */
class AsynchOptImpl
{
public:
    virtual ~AsynchOptImpl ();

    /**
     * Initializes the factory with information which will be used with
     * each asynchronous call. 
     */
    virtual int open (const AsynchHandler *asynch_handler,
	    NDK_HANDLE handle, 
	    const void *completion_key,
	    Proactor *proactor) = 0;

    /**
     * This cancels all pending accepts operations that were issued by
     * the calling thread.  The function does not cancel asynchronous
     * operations issued by other threads.
     */
    virtual int cancel (void) = 0;

    // Return the underlying proactor.
    virtual Proactor* proactor (void) const = 0;
protected:
    // Do-nothing constructor.
    AsynchOptImpl ();
};
// ---------------------------------------------------------------------
/**
 * @class AsynchReadStreamImpl
 *
 */
class AsynchReadStreamImpl : public virtual AsynchOptImpl
{
public:
    virtual ~AsynchReadStreamImpl (void);

    // This starts off an asynchronous read. Upto <bytes_to_read> will
    // be read and stored in the <message_block>.
    virtual int read (MessageBlock *mb,
	    size_t bytes_to_read) = 0;

protected:
    // Do-nothing constructor.
    AsynchReadStreamImpl ();
};

/**
 * @class AsynchReadStreamResultImpl
 *
 */
class AsynchReadStreamResultImpl : public virtual AsynchResultImpl
{
public:
    virtual ~AsynchReadStreamResultImpl ();

    // The number of bytes which were requested at the start of the
    // asynchronous read.
    virtual size_t bytes_to_read (void) const = 0;
    virtual void bytes_to_read (size_t bytes) = 0;

    // Message block which contains the read data.
    virtual MessageBlock *message_block (void) const = 0;
    virtual void message_block (MessageBlock *) = 0;

    // I/O handle used for reading.
    virtual NDK_HANDLE handle (void) const = 0;
protected:
    // Do-nothing constructor.
    AsynchReadStreamResultImpl ();
};

// ---------------------------------------------------------------------
/**
 * @class AsynchWriteStreamImpl
 *
 */
class AsynchWriteStreamImpl : public virtual AsynchOptImpl
{
public:
    virtual ~AsynchWriteStreamImpl ();

    // This starts off an asynchronous write.  Upto <bytes_to_write>
    // will be written from the <message_block>.
    virtual int write (MessageBlock *message_block,
	    size_t bytes_to_write) = 0;
protected:
    // Do-nothing constructor.
    AsynchWriteStreamImpl ();
};

/**
 * @class AsynchWriteStreamResultImpl
 *
 */
class AsynchWriteStreamResultImpl : public virtual AsynchResultImpl
{
public:
    virtual ~AsynchWriteStreamResultImpl ();

    // The number of bytes which were requested at the start of the
    // asynchronous write.
    virtual size_t bytes_to_write (void) const = 0;
    virtual void bytes_to_write (size_t bytes) = 0;

    // Message block that contains the data to be written.
    virtual MessageBlock *message_block (void) const = 0;
    virtual void message_block (MessageBlock *) = 0;

    // I/O handle used for writing.
    virtual NDK_HANDLE handle (void) const = 0;
protected:
    // Do-nothing constructor.
    AsynchWriteStreamResultImpl ();
};
#if 0
// ---------------------------------------------------------------------
/**
 * @class AsynchReadFileImpl
 *
 */
class AsynchReadFileImpl : public virtual AsynchReadStreamImpl
{
public:
    virtual ~AsynchReadFileImpl ();

    /**
     * This starts off an asynchronous read.  Upto <bytes_to_read> will
     * be read and stored in the <message_block>.  The read will start
     * at <offset> from the beginning of the file.
     */
    virtual int read (MessageBlock &message_block,
	    size_t bytes_to_read,
	    long long offset);

protected:
    // Do-nothing constructor.
    AsynchReadFileImpl ();
};

/**
 * @class AsynchReadFileResultImpl
 *
 */
class AsynchReadFileResultImpl : public virtual AsynchReadStreamResultImpl
{
public:
    virtual ~AsynchReadFileResultImpl ();

protected:
    // Do-nothing constructor.
    AsynchReadFileResultImpl ();
};
// ---------------------------------------------------------------------
/**
 * @class AsynchWirteFileImpl
 *
 */
class AsynchWriteFileImpl : public virtual AsynchWriteStreamImpl
{
public:
    virtual ~AsynchWriteFileImpl ();

    /**
     * This starts off an asynchronous write.  Upto <bytes_to_write> will
     * be write and stored in the <message_block>.  The write will start
     * at <offset> from the beginning of the file.
     */
    virtual int write (MessageBlock &message_block,
	    size_t bytes_to_write,
	    long long offset);

protected:
    // Do-nothing constructor.
    AsynchWriteFileImpl ();
};

/**
 * @class AsynchWriteFileResultImpl
 *
 */
class AsynchWriteFileResultImpl : public virtual AsynchWriteStreamResultImpl
{
public:
    virtual ~AsynchWriteFileResultImpl ();

protected:
    // Do-nothing constructor.
    AsynchWriteFileResultImpl ();
};
#endif
// ---------------------------------------------------------------------
/**
 * @class AsynchAcceptImpl
 *
 */
class AsynchAcceptImpl : public virtual AsynchOptImpl
{
public:
    virtual ~AsynchAcceptImpl ();

    /**
     * This starts off an asynchronous read. Upto <num_to_accept> of 
     * connections will be accepted and stored in the ConnectionInfo 
     * that be stored in the <message_block> or occur error.  So the 
     * space of the message_block is must equal or large than 
     * <num_to_accept> times size of ConnectionInfo.
     */
    virtual int accept (MessageBlock *message_block,
	    size_t num_to_accept) = 0;

protected:
    // Do-nothing constructor.
    AsynchAcceptImpl ();
};
/**
 * @class AsynchAcceptResultImpl
 *
 */
class AsynchAcceptResultImpl : public virtual AsynchResultImpl
{
public:
    virtual ~AsynchAcceptResultImpl ();

    // The number of connections which were requested at the start of the
    // asynchronous accept.
    virtual size_t num_to_accept (void) const = 0;
    virtual void num_to_accept (size_t num) = 0;

    // Message block which contains the read data.
    virtual MessageBlock *message_block (void) const = 0;
    virtual void message_block (MessageBlock *) = 0;

    // I/O handle used for accepting new connections.
    virtual NDK_HANDLE listen_handle (void) const = 0;

    // I/O handle for the new connection.
    // virtual NDK_HANDLE accept_handle (void) const = 0; !!!
protected:
    // Do-nothing constructor.
    AsynchAcceptResultImpl ();
};
#include "Post.h"
#endif

